package com.bls.service.impl;

import com.bls.action.BLSAction;
import com.bls.dao.HistoryRecordDao;
import com.bls.entity.HistoryRecord;
import com.bls.service.HistoryRecordService;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * (HistoryRecord)表服务实现类
 *
 * @author makejava
 * @since 2022-01-10 18:43:04
 */
public class HistoryRecordServiceImpl implements HistoryRecordService {
    @Override
    public void save(HistoryRecord historyRecord) throws Exception {
        HistoryRecordDao historyRecordDao = BLSAction.getBean(HistoryRecordDao.class);
        final Connection connection = BLSAction.getDBConnection();

        try {
            historyRecord.setStartTime(new Date());
            historyRecordDao.save(connection, historyRecord);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public HistoryRecord getLastHistoryRecord(Long analyseId) throws Exception {
        HistoryRecordDao historyRecordDao = BLSAction.getBean(HistoryRecordDao.class);
        final Connection connection = BLSAction.getDBConnection();

        return historyRecordDao.getLastHistoryRecord(connection, analyseId);
    }

    @Override
    public List<HistoryRecord> pageConditionHistoryRecordList(HistoryRecord historyRecordQuery, Integer pageSize, Integer current) throws Exception {

        HistoryRecordDao historyRecordDao = BLSAction.getBean(HistoryRecordDao.class);
        final Connection connection = BLSAction.getDBConnection();
        List<HistoryRecord> historyRecordList = null;
        try {
            historyRecordList = historyRecordDao.pageConditionHistoryRecordList(connection, historyRecordQuery, pageSize, current);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return historyRecordList;
    }

    @Override
    public Integer pageConditionHistoryRecordTotal(HistoryRecord historyRecordQuery) throws Exception {

        HistoryRecordDao historyRecordDao = BLSAction.getBean(HistoryRecordDao.class);
        final Connection connection = BLSAction.getDBConnection();

        int count = -1;
        try {
            count = historyRecordDao.pageConditionHistoryRecordTotal(connection, historyRecordQuery);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;

    }

    @Override
    public int update(HistoryRecord historyRecord) throws Exception {
        HistoryRecordDao historyRecordDao = BLSAction.getBean(HistoryRecordDao.class);
        final Connection connection = BLSAction.getDBConnection();

        int i = -1;
        try {
            historyRecord.setEndTime(new Date());
            i = (int) historyRecordDao.updateIgnoreNull(connection, historyRecord);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i;
    }
}